<?php

use Caifenglei\AuthRbac\Http\Controllers\AuthController;
use Caifenglei\AuthRbac\Http\Controllers\PermissionController;
use Caifenglei\AuthRbac\Http\Controllers\RoleController;
use Caifenglei\AuthRbac\Http\Controllers\UserController;
use Caifenglei\AuthRbac\Http\Middleware\Authorize;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::prefix(config('auth-rbac.routes.prefix'))->group(function(){
    Route::controller(AuthController::class)->group(function(){
        Route::post('ulogin', 'login');
        Route::post('login', 'login');
        Route::post('register', 'register');
        Route::post('oauth/callback', 'callback');
    });

    //认证后的接口
    //'middleware:guard'
    $middlewares = ['auth:api', Authorize::class, config('auth-rbac.middlewares.record_log')];
    Route::middleware($middlewares)->group(function(){
        //认证相关接口
        Route::name('auth.')->controller(AuthController::class)->group(function(){
            Route::get('logout', 'logout')->name('logout');
            Route::get('user/info', 'user')->name('user');
            Route::get('refresh', 'refresh')->name('refresh');
        });
        //系统管理
        Route::name('admin.')->group(function (){
            Route::name('users.')->controller(UserController::class)->group(function(){
               Route::get('users', 'index')->name('index');
               Route::get('users/{id}', 'show')->name('show');
               Route::post('users/{id}', 'update')->name('update');
            });
            Route::name('roles.')->controller(RoleController::class)->group(function(){
                Route::get('roles', 'index')->name('index');
                Route::post('roles', 'store')->name('add');
                Route::post('roles_del', 'destroy')->name('delete');
                Route::post('roles/{id}', 'update')->name('update');
                Route::get('roles/{id}', 'show')->name('show');
            });
            Route::name('permissions.')->controller(PermissionController::class)->group(function(){
                Route::get('permissions', 'index')->name('index');
                Route::post('permissions', 'store')->name('add');
                Route::get('permissions/{id}', 'show')->name('show');
                Route::post('permissions/{id}', 'update')->name('update');
                Route::get('permissions_sync', 'sync')->name('sync');
                Route::post('permissions_del', 'destroy')->name('delete');
                Route::get('permissions_granted', 'granted')->name('granted');
            });
        });
    });
});


